

clear all

set more off 



global estimators xtreg

*is it onset (=1) or incidence(=.)?
local cheat 1

if "`cheat'" == "." {
	local on_or_in incidence
}
if "`cheat'" == "1" {
	local on_or_in onset
}

***************************************
***************************************


global models 
forval v = 1 / 21 {
	global models $models `v' `v'w
}


program prepare_roc

	
	local which_thetas ste
	*foreach x in one two {
	foreach x in one {
	
		if "`x'" == "one" {
			local before 1
		}
		if "`x'" == "two" {
			local before 2
		}
																
		*****ll=0 is all countries, 1 only countries that ever had conflict
		forvalues ll = 0/0 {
												

			local model 0
			
			local what 0
			
			*foreach vv in $models {
			foreach vv in 21 21w {
			
				forval ac = 0 / 1 {
				
					clear
					use tempx
					
					keep if ac == `ac'
					
					sort countryid year
	
					gen total_conflict = 0	
	
					tsset countryid year
					generate one_before=conflict==0&F1.conflict==1
					generate two_before=conflict==0&(F1.conflict==1|F2.conflict==1)
	
					if "`cheat'" == "." {
						replace one_before= 1 if conflict==1&F1.conflict==1
						replace two_before= 1 if conflict==1&(F1.conflict==1|F2.conflict==1)
					}
	
	
					drop if year < 1975	
						
					keep if conflict != `2'
					drop if p_`x'_model`vv' == .
					drop if  `x'_before  == .
						
					keep  p_`x'_model`vv' `x'_before total_conflict conflict
				
					local model = `model' + 1 


					sort p_`x'_model`vv'									
					
					egen rank = rank(p_`x'_model`vv') if  p_`x'_model`vv' != .
							
					
					sum rank
					
					local max_rank `r(max)'
					
					
					if `r(N)' > 0 {
				
						local what = `what' + 1
				
						sort p_`x'_model`vv'
					
						sum p_`x'_model`vv'
						replace p_`x'_model`vv' = p_`x'_model`vv' - `r(min)'
						sum p_`x'_model`vv'
						gen ach = `x'_before
						replace	ach  = `r(max)' if `x'_before == 1
						replace ach = `r(min)' if `x'_before == 0
						
						if `what' == 1 {
							local this a) Civil War (overall)
						}
						if `what' == 2 {
						 	local this b) Armed Conflict (overall)
						}
						if `what' == 3 {
							local this c) Civil War (within)
							
						}
						if `what' == 4 {
							local this d) Armed Conflict (within)
						}
					
						twoway  (bar ach rank, color(gs8) ) (line p_`x'_model`vv' rank, color(black) lpattern(dash) ) , ///
						xtitle("") ytitle("")  xtick() xlabel("", notick) ylabel("", notick)  ///
						legend( order(1 "Onset" 2 "Prediction") size(small)) aspectratio(1) ///
						subtitle("`this'", color(black) size(small)) ///
						plotregion(fcolor(white)) graphregion(fcolor(white)) name(fig`what')
					
					
					
					}
				
					if `what' == 4 {
					
						local what 0
											
					
						grc1leg fig1 fig2 fig3 fig4 , legendfrom(fig1) title("", color(black)  size(small)) ///
						subtitle("", color(black) size(small)) plotregion(fcolor(white)) graphregion(fcolor(white)) 
					
						*graph export output/separation`vv'.eps ,replace
						graph export output/fig5.eps ,replace
						
						graph drop _all
					

					}
				}
				
			
				
									
				
			}
			
		}
	}

end



local runs = 0
*forval c = 3(-1)2  {

	local runs = `runs' + 1
	
	*use "output/chris_predict_one_`on_or_in'_int`c'_to15_alpha3_beta001_all_both", clear
	
	use "output/chris_predict_one_`on_or_in'_int3_to15_alpha3_beta001_all_both", clear
	gen ac = 0
	append using "output/chris_predict_one_`on_or_in'_int2_to15_alpha3_beta001_all_both"
	replace ac = 1 if ac == .

	drop conflict
	merge n:1 countryid year using "complete_main_new.dta", keepusing(bdbest25 bdbest1000)
	
	quietly gen conflict = bdbest25 if ac == 1
	quietly replace conflict = bdbest1000 if ac == 0
		

	
	
	

	save tempx, replace
	
	*postfile buffer cut model before conftype tpn fpn tpd fpd using for_roc_`on_or_in'`runs', replace
	prepare_roc 0 `cheat'
	*postclose buffer
	
*}






erase tempx.dta

